package com.gome.ocean;

import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.http.client.methods.HttpGet;
import org.elasticsearch.common.jackson.dataformat.yaml.snakeyaml.util.UriEncoder;

import com.gome.framework.base.BaseTest;
import com.gome.framework.util.JsonUtil;
import com.gome.ocean.common.constants.WebSqlContants;
import com.gome.ocean.common.utils.HttpClientUtil;

import bsh.This;

/**
 * @author zhangguoqing 2016年11月16日 上午10:21:29
 * @since 5.0.0
 */
public class HttpUtils extends BaseTest {

    public static void main(String[] args) throws Exception {
        Map<String, Object> p = new HashMap<>();
        p.put("ptype", "group_db");
        p.put("email", "lisheng@gomeplus.com");
        p.put("env_type", "SIT");
        p.put("gtype", "DEV");
        
        HttpClient httpClient = new HttpClient();
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(1000);
        httpClient.getHttpConnectionManager().getParams().setSoTimeout(5000);
//
        GetMethod method = new GetMethod("http://10.126.53.192/mcenter/websql/api/privilege?pargs="+URLEncoder.encode(JsonUtil.toJson(p),"UTF-8"));
        System.out.println(URLEncoder.encode(JsonUtil.toJson(p),"UTF-8"));
        System.out.println(URLEncoder.encode("{\"gtype\":\"DEV\",\"ptype\":\"group_db\",\"env_type\":\"SIT\",\"email\":\"lisheng@gomeplus.com\"}","UTF-8"));
//        
        int statusCode = httpClient.executeMethod(method);
        System.out.println(statusCode);
        if (statusCode == 200) {
            System.out.println(method.getResponseBodyAsString());
        }

        HttpClientUtil clientUtil = new HttpClientUtil();
        HttpGet httpGet = HttpClientUtil.getGetRequest();
        String path = WebSqlContants.getFormatAuthorityGroupPathStr("10.126.53.192", URLEncoder.encode(JsonUtil.toJson(p),"UTF-8"));
        URL url = new URL(path);
        httpGet.setURI(url.toURI());
        String json = clientUtil.executeAndGetWithFailedRetry(httpGet, 6, 10001);
        System.out.println(json);

    }
}
